<?php

namespace app\data\controller\record;

use think\admin\Controller;

/**
 * 车型计费
 * Class VehicleType
 * @package app\data\controller\record
 */
class VehicleType extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'car_vehicle_types';

    /**
     * 车型管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '车型管理';
        $query = $this->_query($this->table);
        $query->like('name');
        $query->order('sort desc,id asc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as &$item) {
            $item['match_text'] = implode(',', array_filter(explode(',', $item['match_text'])));
        }
    }

    /**
     * 添加车型
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑车型
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            if (!empty($data['match_text'])) {
                $data['match_text_array'] = array_filter(explode(',', $data['match_text']));
            }
        } else {
            if (isset($data['id'])) {
                $oldData = $this->app->db->name('car_vehicle_types')->where('id', $data['id'])->find();
                if (empty($oldData)) {
                    $this->error('查无该车型');
                }
                if (empty($data['match_text'])) {
                    $this->error('匹配词不能为空');
                }

                $data['match_text'] = ',' . implode(',', array_filter($data['match_text'])) . ',';
                $has = $this->app->db->name('car_vehicle_types')->where('id', '<>', $data['id'])->where('name', $data['name'])->find();
            } else {
                $has = $this->app->db->name('car_vehicle_types')->where('name', $data['name'])->find();
            }

            if ($has) {
                $this->error('车型名称不能重复配置');
            }
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功');
        }
    }

    /**
     * 删除车型
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $id = $this->request->request('id');
        $oldData = $this->app->db->name('car_vehicle_types')->where('id', $id)->find();
        if (empty($oldData)) {
            $this->error('查无该车型');
        }

        $this->_delete($this->table);
    }

    /**
     * 车型默认配置
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function config()
    {
        $this->title = '车型默认配置';
        $this->__sysconf('config');
    }

    /**
     * 显示并保存配置
     * @param string $template 模板文件名称
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function __sysconf(string $template)
    {
        if ($this->request->isGet()) {
            $this->fetch($template);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k => $v) {
                sysconf($k, $v);
            }
            $this->success('配置保存成功！');
        }
    }

    /**
     * 平台价格编辑
     * @throws \think\db\exception\DbException
     */
    public function platformPrice()
    {

    }

}